import Client from "../utils/sms.js";
import Core from "@alicloud/pop-core"
import {v4 as uuidv4} from "uuid"
import redisClient from "../redis.js";
import {setToken} from "../utils/token.js";
import {generateVerificationCode} from "../utils/uuid.js";

export async function sendMessage(event, req, res){
    // await Client.main(process.argv.slice(2), event.phoneNumbers, event.code)
    // 生成四位随机数验证码
    const verificationCode = generateVerificationCode()

    let client = new Core({
        // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
        accessKeyId: "LTAI5tD9C4LGEZREweE85U83",
        accessKeySecret: "L0dOo1XCJeIrY878b52CAAPuSLDj2Z",
        // securityToken: process.env['ALIBABA_CLOUD_SECURITY_TOKEN'], // use STS Token
        endpoint: 'https://dysmsapi.aliyuncs.com',
        apiVersion: '2017-05-25'
    });

    let params = {
        "SignName": "campube",
        "PhoneNumbers": `${event.phoneNumbers}`,
        "TemplateCode": "SMS_460751147",
        "TemplateParam": `{\"code\":\"${verificationCode}\"}`
    }

    let requestOption = {
        method: 'POST',
        formatParams: false,

    };

    client.request('SendSms', params, requestOption).then((result) => {
        console.log(JSON.stringify(result));
    }, (ex) => {
        console.log(ex);
    })

    // 设置验证码时效
    await setToken(verificationCode, true, "EX", 300)

    return verificationCode
}

